import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import script.IScript;
import test.IPlayerLoginScript;
import test.IPlayerRunScript;
import test.PlayerManager;
import test.impl.PlayerLoginAndRunScript;
import test.impl.PlayerLoginScript;
import test.impl.PlayerManagerScript;
import test.impl.PlayerRunScript;
import test.impl.PlayerScript;
import util.ReflectUtil;
import util.ScriptUtil;

/**
 * 文 件 名:  Test.java
 * 工 程 名:  ScriptManager
 * 创建日期:  2015年4月2日 下午5:17:52
 * 创建作者:  杨 强  <281455776@qq.com>
 */

/**
 * @author 杨 强
 *
 */
public class Test {

    public static void main(String[] args) {
        if (args != null && args.length > 1) {
            ScriptUtil.setScriptRootPath(args[0]);
            ScriptUtil.setScriptJarFileName(args[1]);
        }
        ScriptUtil.loadScript(PlayerLoginScript.getScript());
        ScriptUtil.loadScript(PlayerRunScript.getScript());
        ScriptUtil.loadScript(PlayerLoginAndRunScript.getScript());
        ScriptUtil.loadScript(PlayerScript.getScript());
        ScriptUtil.loadScript(PlayerManagerScript.getScript());
        test1();
    }

    public static void test1() {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                List<IPlayerLoginScript> scripts = ScriptUtil.getEventScripts(IPlayerLoginScript.class);
                for (IPlayerLoginScript script : scripts) {
                    script.login();
                }
                // ScriptUtil.triggerEvent(IPlayerLoginScript.class);
            }
        }, 200, 500, TimeUnit.MILLISECONDS);
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                // List<IPlayerRunScript> scripts = ScriptUtil.getEventScripts(IPlayerRunScript.class);
                // for (IPlayerRunScript script : scripts) {
                // script.run(2);
                // }
                ScriptUtil.triggerEvent(IPlayerRunScript.class, 2);
            }
        }, 300, 500, TimeUnit.MILLISECONDS);
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                IScript script = ScriptUtil.getLoadedScript(PlayerScript.class);
                ReflectUtil.invokeMethod(script, "show", null);
            }
        }, 400, 500, TimeUnit.MILLISECONDS);
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                PlayerManager.getInstance().getPlayerName();
            }
        }, 500, 500, TimeUnit.MILLISECONDS);
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ScriptUtil.reloadAllScripts();
                // ScriptUtil.reloadScript("test.impl.PlayerManagerScript");
            }
        }, 5, 5, TimeUnit.SECONDS);
    }
}
